/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.itver.gui.visual;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.logging.Logger;
import javax.swing.JPanel;
import org.itver.graphics.model.Universe;
import org.itver.gui.listeners.MainFrameMouseListener;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;
import org.netbeans.spi.palette.PaletteController;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.itver.gui.visual//MainFrame//EN",
autostore = false)
public final class MainFrameTopComponent extends TopComponent {

    //Se usa en la paleta
    private InstanceContent content;

    private static MainFrameTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    private static final String PREFERRED_ID = "MainFrameTopComponent";

    public MainFrameTopComponent() {
        /*
         * Para relacionar el TopComponent con una o mas paletas, se debe relacionar 
         * mediante InstanceCOntent. que tiene el metodo instancecontent.
         */
        this(new InstanceContent());
    }

    private MainFrameTopComponent(InstanceContent content){
        super(new AbstractLookup(content));
        initComponents();
        this.content = content;
        init3D();
//        add(Universe.getInstance().getCanvas(), BorderLayout.CENTER);
        setName(NbBundle.getMessage(MainFrameTopComponent.class, "CTL_MainFrameTopComponent"));
        setToolTipText(NbBundle.getMessage(MainFrameTopComponent.class, "HINT_MainFrameTopComponent"));
//        setIcon(ImageUtilities.loadImage(ICON_PATH, true));
        putClientProperty(TopComponent.PROP_DRAGGING_DISABLED, Boolean.TRUE);
        putClientProperty(TopComponent.PROP_UNDOCKING_DISABLED, Boolean.TRUE);
//        EnvironmentLimits.getInstance().calculateUniverseBounds();

        MainFrameMouseListener mouseListener = new MainFrameMouseListener(Universe.getInstance().getScene(), Universe.getInstance().getCanvas());
        Universe.getInstance().getCanvas().addMouseListener(mouseListener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setPreferredSize(new java.awt.Dimension(500, 500));
        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized MainFrameTopComponent getDefault() {
        if (instance == null) {
            instance = new MainFrameTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the MainFrameTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized MainFrameTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(MainFrameTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof MainFrameTopComponent) {
            return (MainFrameTopComponent) win;
        }
        Logger.getLogger(MainFrameTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID
                + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    //Lo que estaba en el constructor principal se movio aqui. La inicializacion
    //de Java 3D
    private void init3D(){
        JPanel panelCanvas = new JPanel();
        panelCanvas.setPreferredSize(new Dimension(500,500));
        panelCanvas.setSize(500, 500);
        panelCanvas.setLayout(new BorderLayout());
        panelCanvas.add(Universe.getInstance().getCanvas(), BorderLayout.CENTER);
        add(panelCanvas);
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    Object readProperties(java.util.Properties p) {
        if (instance == null) {
            instance = this;
        }
        instance.readPropertiesImpl(p);
        return instance;
    }

    private void readPropertiesImpl(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }
    
    /*
     *Pablo.
     * TODO: Agrega un controlador de paleta dentro del InstanceContent del 
     * TopComponent.
     */
    public void addPaletteController(PaletteController controller){
        this.content.add(controller);
    }
    
    /*
     * Pablo.
     * TODO: quita un controlador de paleta del instanceContent del TopComponent
     */
    public void removePaletteController(PaletteController controller){
        this.content.remove(controller);
    }


}
